package vip.xiaonuo.vein.impl.member;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vip.xiaonuo.facedoor.iservice.IMemberDealWithService;
import vip.xiaonuo.facedoor.iservice.entity.PersonBaseParam;
import vip.xiaonuo.facedoor.iservice.entity.PersonBaseParamRet;
import vip.xiaonuo.facedoor.iservice.issulog.DeviceLogService;
import vip.xiaonuo.facedoor.modular.devicebase.entity.DeviceBase;
import vip.xiaonuo.facedoor.modular.memberdevice.constant.MemberDeviceConstant;
import vip.xiaonuo.facedoor.modular.memberdevice.entity.MemberDevice;
import vip.xiaonuo.facedoor.modular.memberdevice.service.MemberDeviceService;
import vip.xiaonuo.facedoor.modular.memberuser.entity.MemberUser;
import vip.xiaonuo.facedoor.remote.iservice.adapter.MemberReqAdapter;
import vip.xiaonuo.facedoor.rule.RuleConstant;
import vip.xiaonuo.facedoor.rule.VeinRulesEngine;
import vip.xiaonuo.vein.impl.adapter.VeinMemberReqAdapter;

@Service(RuleConstant.VEIN+ RuleConstant.MEMBER_DEAL_WITH)
public class VeinMemberDealWithServiceImpl implements IMemberDealWithService {

  @Autowired
  private VeinMemberReqAdapter memberReqAdapter;

  @Autowired
  private MemberDeviceService memberDeviceService;

  @Autowired
  private DeviceLogService deviceLogService;


  @Override
  public PersonBaseParamRet getBaseParam(PersonBaseParam personBasePerson) {
    return new PersonBaseParamRet().setUrl("http://" + personBasePerson.getDeviceBase().getCabinetIp() + ":" + personBasePerson.getDeviceBase().getCabinetPort());
  }

  @Override
  public void deleteException(MemberDevice memberDevice, MemberUser memberUser, DeviceBase deviceBase, Exception e) {
    memberDevice.setFailReason(ExceptionUtils.getMessage(e));
    memberDevice.setStatus(MemberDeviceConstant.DELETE_FAIL);
    memberDeviceService.updateById(memberDevice);
    deviceLogService.addMemberLog(memberUser.getName(), deviceBase.getName(), ExceptionUtils.getMessage(e), FAIL, DELETE);
  }

  @Override
  public void delete(MemberDevice memberDevice, MemberUser memberUser, PersonBaseParamRet personBaseParamRet, DeviceBase deviceBase) throws Exception {
    memberUser.setMemberType(1);
    memberUser.setMemberOrVisitorDeviceId(memberDevice.getId());
    memberReqAdapter.deletePerson(memberDevice, memberDevice.getDeviceId() + "");
  }


  @Override
  public void add(MemberDevice memberDevice, MemberUser memberUser, PersonBaseParamRet personBaseParamRet, DeviceBase deviceBase) throws Exception {
    memberUser.setMemberType(1);
    memberUser.setMemberOrVisitorDeviceId(memberDevice.getId());
    memberReqAdapter.addPerson(memberDevice.getDeviceId() + "", memberUser);
  }


  @Override
  public void addException(MemberDevice memberDevice, MemberUser memberUser, DeviceBase deviceBase, Exception e) {
    // 直接修改为失败
    memberDevice.setFailReason(JSON.toJSONString(ExceptionUtils.getMessage(e)));
    memberDevice.setStatus(MemberDeviceConstant.ADD_FAIL);
    memberDeviceService.updateById(memberDevice);
    deviceLogService.addMemberLog(memberUser.getName(), deviceBase.getName(), ExceptionUtils.getMessage(e), FAIL, ADD);
  }

  @Override
  public void updateException(MemberDevice memberDevice, MemberUser memberUser, DeviceBase deviceBase, Exception e) {
    // 直接修改为失败
    memberDevice.setFailReason(JSON.toJSONString(ExceptionUtils.getMessage(e)));
    memberDevice.setStatus(MemberDeviceConstant.UPDATE_FAIL);
    memberDeviceService.updateById(memberDevice);
    deviceLogService.addMemberLog(memberUser.getName(), deviceBase.getName(), ExceptionUtils.getMessage(e), FAIL, UPDATE);
  }

  @Override
  public void update(MemberDevice memberDevice, MemberUser memberUser, PersonBaseParamRet baseParam, DeviceBase deviceBase) throws Exception {
    memberUser.setMemberType(1);
    memberUser.setMemberOrVisitorDeviceId(memberDevice.getId());
    memberReqAdapter.updatePerson(memberDevice.getDeviceId() + "", memberUser);
  }
}
